<?php

namespace app\admin\controller;
use app\models\CompanyModel;
use app\models\RegionalModel;
use app\models\UserModel;

/**
 * 区域管理
 * User: Small Y
 * Date: 2017/03/13
 * @package app\admin\controller
 */
class Regional extends Common
{
    /**
     * 用户管理模型.
     *
     * @var UserModel
     */
    protected $userModel;

	/**
	 * 区域管理模型.
	 *
	 *  @var RegionalModel
	 */
	protected $regionalModel;

	/**
	 * 集团管理模型.
	 *
	 * @var CompanyModel
	 */
    protected $companyModel;

    /**
     * 区域管理.
     *
     * @throws \think\exception\DbException
     *
     * @return mixed
     */
    public function lists()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $regional = $this->regionalModel->getRegionalLists($param);
            $this->success('success', '', $regional, 0);
        }

        return $this->fetch();
    }

	/**
	 * 添加区域.
	 * modify by Small Y
	 *
	 * @return mixed
	 * @throws \think\exception\DbException
	 */
    public function regional_add()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');

            if($this->regionalModel->get(['company_id'=>$data['company_id'],'regional_name'=>$data['regional_name']])){
				$this->error('区域已存在', null, '', 1);
			}
            /********更新集团区域regional_id***********/
            $result_user = $this->userModel->get(['company_id' => $data['company_id'],'wtype' => 2]);
            if (false !== $this->regionalModel->save($data)) {
                if($result_user['regional_id'] != null && strlen($result_user['regional_id']) > 0){
                    $ids = $this->regionalModel->get(['company_id' => $data['company_id'],'regional_name'=>$data['regional_name']]);
                    $result_user['regional_id'] = $result_user['regional_id'].','.$ids['id'];
                    if(!$this->userModel->where(['company_id' => $data['company_id'],'wtype' => 2])->update(['regional_id' => $result_user['regional_id']])){
                        $this->error('更新区域ID失败', null, '', 1);
                    }
                }else{
                    $ids = $this->regionalModel->get(['company_id' => $data['company_id'],'regional_name'=>$data['regional_name']]);
                    if(!$this->userModel->where(['company_id' => $data['company_id'],'wtype' => 2])->update(['regional_id' => $ids['id']])){
                        $this->error('更新区域ID失败', null, '', 1);
                    }
                }

                $this->success('添加成功', url('Regional/lists'), '', 1);
            }

            $this->error('添加失败', null, '', 1);
        }
		$where = [];
		$where['status'] = 1;
		if(get_current_type() != 1){
			$where['id'] = get_current_company_id();
		}else if(get_current_type() == 1){
		    /*************过滤掉未分配系统账号的集团*************/
            $user_result = $this->userModel->where(['wtype' => 2])->select();
            $company_user_arr = [];
            foreach ($user_result as $key => $value){
                $company_user_arr[] = $value['company_id'];
            }
            $company_user_arr = array_unique($company_user_arr);
            $company_user_str = implode(',',$company_user_arr);
            $where['id'] = ['in',$company_user_str];
        }
		$companys = $this->companyModel->all($where);
		return $this->fetch('',['companys'=>$companys]);
    }

    /**
     * 编辑区域.
     *
     * @return mixed
     */
    public function regional_edit()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if (!$this->regionalModel->save($data, ['id' => $data['id']])) {
                $this->error('编辑失败', null, '', 1);
            }
            $this->success('编辑成功', url('Regional/lists'), '', 1);
        }
    }

    /**
     * 更改区域状态.
     *
     * @return mixed
     */
    public function regional_status()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if($data['status'] == 1){
                $msg = '启用';
            }else{
                $msg = '禁用';
            }
            if (!$this->regionalModel->save($data, ['id' => $data['id']])) {
                $this->error($msg.'失败', null, '', 1);
            }
            $this->success($msg.'成功', '', '', 1);
        }
    }

	/**
	 * 实例化.
	 * modify by Small Y
	 *
	 * @return bool|void
	 */
    protected function _initialize()
    {
        parent::_initialize();
        $this->regionalModel = new RegionalModel();
        $this->companyModel = new CompanyModel();
        $this->userModel = new UserModel();
    }
}
